//
//  ZhuanTiViewController.m
//  半糖
//
//  Created by RenShuang on 15-9-2.
//  Copyright (c) 2015年 shuang. All rights reserved.
//

#import "ZhuanTiViewController.h"
#import "AFNBlock.h"
#import "firstTableModel.h"
#import "firstTableViewCell.h"
#import "TableDetailViewController.h"
#import "MJRefresh.h"
#import "BaseHeader.h"

@interface ZhuanTiViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,retain)UITableView *myTableView;
@property(nonatomic,retain)NSMutableArray *dataArr;
@property(nonatomic,assign)NSInteger foot;

@end

@implementation ZhuanTiViewController

- (void)viewWillAppear:(BOOL)animated
{
    [self.myTableView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    NSLog(@"%ld",(long)self.number);
    self.view.backgroundColor = [UIColor whiteColor];
    _dataArr = [[NSMutableArray alloc] init];
    _foot = 0;
    
    _myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    _myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _myTableView.delegate = self;
    _myTableView.dataSource = self;
    [self.view addSubview:_myTableView];
    [self footerFresh];
    
    [self.myTableView addLegendHeaderWithRefreshingBlock:^{
        [self.myTableView.header endRefreshing];
    }];
    [self.myTableView addLegendFooterWithRefreshingTarget:self refreshingAction:@selector(footerFresh)];
}

- (void)footerFresh
{
    NSString *url_str = [NSString stringWithFormat:@"http://open3.bantangapp.com/topic/list?app_installtime=1440915950.090587&app_versions=4.1.1&category=%ld&channel_name=appStore&client_id=bt_app_ios&client_secret=9c1e6634ce1c5098e056628cd66a17a5&oauth_token=6de3bace25123737e17db3e839477eb1&os_versions=8.2&page=%ld&pagesize=20&screensize=640&track_device_info=iPhone&track_deviceid=B4391E1B-981F-4473-A960-431F69E76E8C&track_user_id=1260331&v=6",self.number,_foot];
    
    [AFNBlock getDataWithString:url_str block:^(id block) {
        NSMutableDictionary *dic = block;
        NSArray *arr = [[dic objectForKey:@"data"] objectForKey:@"topic"];
        for (NSDictionary *dic in arr) {
            firstTableModel *model = [[firstTableModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            model.cid = [dic objectForKey:@"id"];
            [_dataArr addObject:model];
            [model release];
            }
       

        NSLog(@"%ld",_dataArr.count);
    }];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        _foot++;
        [self.myTableView.footer endRefreshing];
        [_myTableView reloadData];
    });
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return SCREEN_HEIGHT*7/24;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellid = @"cell";
    firstTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
    if (cell == nil) {
        cell = [[firstTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
    }
    cell.contentView.backgroundColor = [NightHandle shareHandle].viewColor;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    firstTableModel *model = [_dataArr objectAtIndex:indexPath.row];
    [cell getcell:model];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    CommentViewController *comment = [[CommentViewController alloc] init];
    firstTableModel *model = [self.dataArr objectAtIndex:indexPath.row];
    comment.urlStr = [NSString stringWithFormat:@"http://m.ibantang.com/topic/detail/%@/",model.cid];
    [self.navigationController pushViewController:comment animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
